package com.zl.web.controller.classroommanagement;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.zl.classroommanagement.domain.QrcdeUser;
import com.zl.classroommanagement.service.IQrcdeUserService;
import com.zl.classroommanagement.service.IStartcallService;
import com.zl.common.core.domain.entity.SysUser;
import com.zl.common.utils.QRCodeUtils;
import com.zl.common.utils.sign.Md5Utils;
import com.zl.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zl.common.annotation.Log;
import com.zl.common.core.controller.BaseController;
import com.zl.common.core.domain.AjaxResult;
import com.zl.common.enums.BusinessType;
import com.zl.classroommanagement.domain.Startcall;
import com.zl.common.utils.poi.ExcelUtil;
import com.zl.common.core.page.TableDataInfo;

/**
 * 开始点名Controller
 *
 * @author zl
 * @date 2022-04-23
 */
@RestController
@RequestMapping("/classroommanagement/startcall")
public class StartcallController extends BaseController
{
    @Autowired
    private IStartcallService startcallService;

    @Autowired
    private IQrcdeUserService qrcdeUserService;

    @Autowired
    private ISysUserService userService;

    @PostMapping("createQrcode")
    public void getQrcode(@RequestBody Startcall qrcodeVo) throws Exception {
        String text =qrcodeVo.getTeacher()+qrcodeVo.getTeachercard()+qrcodeVo.getClasss();
        String logoPath = "F:\\1646309274617.jpeg";
        String destPath = "F:\\";
        String id = Md5Utils.hash(text);
        byte[] data = QRCodeUtils.encodeD(text, logoPath, destPath, true);
        qrcodeVo.setId(id);
        qrcodeVo.setQrimg(data);
        qrcodeVo.setQrData(new Date());
        startcallService.insertStartcall(qrcodeVo);
        SysUser user = new SysUser();
        QrcdeUser qrcdeUser = new QrcdeUser();
        user.setDeptId(Long.valueOf(qrcodeVo.getClasss()));
        List<SysUser> list = userService.selectUserList(user);
        list.stream().forEach(a->{
            qrcdeUser.setClasss(a.getDeptId());
            qrcdeUser.setNickName(a.getNickName());
            qrcdeUser.setUserAme(a.getUserName());
            qrcdeUser.setTeachercard(qrcodeVo.getTeachercard());
            qrcdeUser.setPublishDate(qrcodeVo.getQrData());
            qrcdeUserService.insertQrcdeUser(qrcdeUser);
        });

    }


    /**
     * 获取开始点名详细信息
     */
    @PostMapping(value = "/getInfos")
    public AjaxResult getInfos(@RequestBody Startcall qrcodeVo)
    {
        String text =qrcodeVo.getTeacher()+qrcodeVo.getTeachercard()+qrcodeVo.getClasss();
        String id = Md5Utils.hash(text);
        return AjaxResult.success(startcallService.selectStartcallById(id));
    }

    /**
     * 查询开始点名列表
     */
    @PreAuthorize("@ss.hasPermi('classroommanagement:startcall:list')")
    @GetMapping("/list")
    public TableDataInfo list(Startcall startcall)
    {
        startPage();
        List<Startcall> list = startcallService.selectStartcallList(startcall);
        return getDataTable(list);
    }

    /**
     * 导出开始点名列表
     */
    @PreAuthorize("@ss.hasPermi('classroommanagement:startcall:export')")
    @Log(title = "开始点名", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Startcall startcall)
    {
        List<Startcall> list = startcallService.selectStartcallList(startcall);
        ExcelUtil<Startcall> util = new ExcelUtil<Startcall>(Startcall.class);
        util.exportExcel(response, list, "开始点名数据");
    }

    /**
     * 获取开始点名详细信息
     */
    @PreAuthorize("@ss.hasPermi('classroommanagement:startcall:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(startcallService.selectStartcallById(id));
    }

    /**
     * 新增开始点名
     */
    @PreAuthorize("@ss.hasPermi('classroommanagement:startcall:add')")
    @Log(title = "开始点名", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Startcall startcall)
    {
        return toAjax(startcallService.insertStartcall(startcall));
    }

    /**
     * 修改开始点名
     */
    @PreAuthorize("@ss.hasPermi('classroommanagement:startcall:edit')")
    @Log(title = "开始点名", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Startcall startcall)
    {
        return toAjax(startcallService.updateStartcall(startcall));
    }

    /**
     * 删除开始点名
     */
    @PreAuthorize("@ss.hasPermi('classroommanagement:startcall:remove')")
    @Log(title = "开始点名", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(startcallService.deleteStartcallByIds(ids));
    }
}
